package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.EditText;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProductCatalogListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProductLedgerOriginal;
import com.isunland.managesystem.entity.ProductLedgerOriginal.ProductLedgerContent;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ProductCatalogListFragment extends BaseListFragment {

  private ProductCatalogListAdapter mAdapter;
  private ProductLedgerContent mContent;
  private ArrayList<ProductLedgerContent> mlist;
  private CurrentUser mCurrentUser = CurrentUser.newInstance(getActivity());
  private EditText etproductName;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.product_list);
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_search_work_area, null,
            false);
    etproductName = (EditText) header.findViewById(R.id.et_search_searchPersonHeader);
    ImageSpan imageHint = new ImageSpan(mActivity, R.drawable.ic_search_gray_64);
    SpannableString spannableString =
        new SpannableString("   请输入产品名称");
    spannableString.setSpan(imageHint, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    etproductName.setHint(spannableString);
    etproductName.setHint(R.string.productHint);
    etproductName.addTextChangedListener(new MyTextWatcher());
    mListview.addHeaderView(header);
  }

  //	@Override
  //	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  //		inflater.inflate(R.menu.menu_contacts_search, menu);
  //		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  //			MenuItem searchItem = menu.findItem(R.id.menu_item_search);
  //			SearchView searchView = (SearchView) searchItem.getActionView();
  //
  //			SearchManager searchManager = (SearchManager) getActivity()
  //					.getSystemService(Context.SEARCH_SERVICE);
  //			ComponentName name = getActivity().getComponentName();
  //			SearchableInfo searchInfo = searchManager.getSearchableInfo(name);
  //			searchView.setSearchableInfo(searchInfo);
  //			searchView.setOnQueryTextListener(new OnQueryTextListener() {
  //
  //				@Override
  //				public boolean onQueryTextSubmit(String query) {
  //
  //					return false;
  //				}
  //
  //				@Override
  //				public boolean onQueryTextChange(String newText) {
  //
  //					if (TextUtils.isEmpty(newText)) {
  //						setAdapter(mlist);
  //					}else{
  //						updateItems(newText);
  //					}
  //
  //					return true;
  //				}
  //			});
  //		}
  //
  //		super.onCreateOptionsMenu(menu, inflater);
  //	}

  public void updateItems(String query) {

    // 直接通过中文人名来进行搜索
    ArrayList<ProductLedgerContent> listNames = new ArrayList<>();// 获取人名
    String str;
    for (int i = 0; i < mlist.size(); i++) {
      str = mlist.get(i).getProductName();
      if (str.contains(query)) {
        listNames.add(mlist.get(i));
      }
    }

    if (listNames.size() == 0) {
      // 弹出提示
      ToastUtil.show(R.string.noProduct);
    }
    //刷新列表
    setAdapter(listNames);
    etproductName.requestFocus();
  }

  @Override public String getUrl() {
    return ApiConst.URL_PRODUCT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", mCurrentUser.getMemberCode());
    params.put("dataStatus", "publish");
    params.put("materialKindCodeLikeStr", "05");
    params.put("mobileQuery", "T");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    ProductLedgerOriginal original = new Gson().fromJson(arg0, ProductLedgerOriginal.class);
    List<ProductLedgerContent> rows = original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<ProductLedgerContent> mlist) {
    if (null == mlist) {
      mlist = new ArrayList<>();
    }
    // 将mtasks放入适配器显示
    mAdapter = new ProductCatalogListAdapter(mActivity, mlist);
    setListAdapter(mAdapter);
    ((ProductCatalogListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  /**
   * 文字监听
   */
  private class MyTextWatcher implements TextWatcher {

    public MyTextWatcher() {
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
      if (mlist == null || mlist.size() == 0) {
        return;
      }
      String query = etproductName.getText().toString().trim();
      if (TextUtils.isEmpty(query)) {
        setAdapter(mlist);
        etproductName.requestFocus();
      } else {
        updateItems(query);
      }
    }

    @Override public void afterTextChanged(Editable editable) {

    }
  }
}

